"""
============================
 _*_coding : UTF-8 _*_
 Team ： Administrator
 Personnel ： Kyh
 Data ： 2020/2/22  16:06
 File ： 爬取百度文库资源.py
 Developer Kits ： PyCharm
============================
"""
'''
借助两个百度文库ppt，txt操作
灵感来源地址：https://wenku.baidu.com/view/2b7046e3f78a6529657d5376.html  #ppt
            https://wenku.baidu.com/view/cbb4af8b783e0912a3162a89.html  #txt

'''
import re
import os
import requests
import json

session = requests.session()


# 发送请求，获取内容
def fetch_url(url):
    # print(session.get(url).content.decode('gbk'))
    return session.get(url).content.decode('gbk')


def get_doc_id(url):
    # https://wenku.baidu.com/view/cbb4af8b783e0912a3162a89.html?from=search
    return re.findall('view/(.*?).html', url)[0]


# 文档类型
def parser_type(content):  # .*?  代表匹配除换行符以外的一切字符
    return re.findall(r"docType.*?\:.*?\'(.*?)\'\,", content)[0]  # 正则表达式          #'docType': 'txt', // 文档格式


# 标题
def parser_title(content):
    return re.findall(r"title.*?\:.*?\'(.*?)\'\,", content)[0]  # 'title': '2015春华师《应用统计学A》在线作业答案', // 文档标题


# md5数据获取
def parser_txt(doc_id):
    content_url = 'https://wenku.baidu.com/api/doc/getdocinfo?callback=cb&doc_id=' + doc_id  # 这一部分不影响可去除&t=1582276400586&_=1582276400323'
    content = fetch_url(content_url)
    # md5值（从正则表达式找）
    md5 = re.findall('"md5sum":"(.*?)"', content)[0]
    # print(md5)
    # 页码   "totalPageNum":"4"
    pn = re.findall('"totalPageNum":"(.*?)"', content)[0]
    # rsign  "p_4-r_0-s_460f0"
    rsign = re.findall('"rsign":"(.*?)"', content)[0]
    # 仔细看代码拆分
    content_urls = 'https://wkretype.bdimg.com/retype/text/' + doc_id + '?rn=' + pn + '& type = txt ' + md5 + '&rsign=' + rsign

    content = json.loads(fetch_url(content_urls))
    result = ''
    for item in content:
        for i in item['parags']:
            result += i['c'].replace('\\r', '\r').replace('\\n', '\n')
    return result


def parse_other(doc_id):
    # 在XHR条件下找
    '''
    https://wenku.baidu.com/browse/getbcsurl?doc_id=2b7046e3f78a6529657d5376&pn=1&rn=99999&type=ppt&callback=jQuery11010697954727474899_1582283326140&_=1582283326141

    '''
    content_url = 'https://wenku.baidu.com/browse/getbcsurl?doc_id=' + doc_id + '&pn=1&rn=99999&type=ppt'
    content = fetch_url(content_url)

    url_list = re.findall('{"zoom":"(.*?)","page"', content)
    url_list = [item.replace('\\', '') for item in url_list]
    if not os.path.exists(doc_id):
        os.mkdir(doc_id)
        for index, url in enumerate(url_list):
            content = session.get(url).content
            path = os.path.join(doc_id, str(index) + '.jpg')
            with open(path, 'wb') as f:
                f.write(content)
        print("图片保存在" + doc_id + "文件夹")


# 保存数据
def save_file(filename, content):
    with open(filename, 'w', encoding='utf-8') as f:
        f.write(content)
        print("已保存为：" + filename)


# 主函数
def main():
    url = input("请输入要下载的文库Url地址")  # 如果加上：会直接弹出网页

    # 请求
    content = fetch_url(url)
    # 获取id
    doc_id = get_doc_id(url)
    # 文档类型
    type = parser_type(content)
    # print(type)
    # 标题
    title = parser_title(content)
    # print(title)
    if type == 'txt':
        result = parser_txt(doc_id)
        save_file(title + '.txt', result)
    else:
        parse_other(doc_id)


if __name__ == '__main__':
    main()
# https://wenku.baidu.com/api/doc/getdocinfo?callback=cb&doc_id=cbb4af8b783e0912a3162a89&t=1582276400586&_=1582276400323

'''https://wkretype.bdimg.com/retype/text/cbb4af8b783e0912a3162a89?
md5sum=6e7a10b16f3ad8d3b40ecd0dfe8d1b67&sign=f784f3cb76
&callback=cb&pn=1&rn=4
&type=txt
&rsign=p_4-r_0-s_460f0&_=1582276400324'''
